package com.youkeda.app.dataobject;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.youkeda.app.model.Category;
import com.youkeda.app.util.UUIDUtils;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;


import java.time.LocalDateTime;

/**
 * @ClassName CategoryDO
 * @Author 刘正星
 * @Date 2020/8/20 15:15
 **/
@Data
public class CategoryDO {

    //主键
    private String id;
    //类目名称
    private String name;
    //类目描述
    private String description;
    //父类目id
    private String parentCategoryId;
    //创建时间
    @JsonFormat(shape =JsonFormat.Shape.STRING ,pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtCreated;
    //修改时间
    @JsonFormat(shape =JsonFormat.Shape.STRING ,pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime gmtModified;
    public CategoryDO() {
    }

    public CategoryDO(Category category) {
        BeanUtils.copyProperties(category, this);
        if (StringUtils.isEmpty(category.getId())) {
            this.setId(UUIDUtils.getUUID());
        }
        if (!StringUtils.isEmpty(category.getParentCategoryId())) {
            this.setParentCategoryId(category.getParentCategoryId());
        }
    }

    public Category convertToModel() {
        Category category = new Category();
        BeanUtils.copyProperties(this, category);
        if (!StringUtils.isEmpty(this.getParentCategoryId())) {
            category.setParentCategoryId(this.getParentCategoryId());
        }
        return category;
    }

}
